package practises;

public class Num7 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(4);
        head.next.next = new ListNode(2);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(1);
        Num7 n = new Num7();
        System.out.println(n.chkPalindrome(head));
        int k = 1;
    }
    public boolean chkPalindrome(ListNode A) {
        if(A == null) {
            return false;
        }
        ListNode head = new ListNode(-1);
        head.next = A;
        ListNode slow = head;
        ListNode fast = head;
        //找到中间节点的位置
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode prev = slow;
        ListNode cur = slow.next;
        ListNode next = cur.next;
        prev.next = null;
        //将中间节点以后的节点进行翻转
        while(cur.next != null) {
            cur.next = prev;
            prev = cur;
            cur = next;
            next = next.next;
        }
        cur.next = prev;
        ListNode curTail = cur;
        ListNode curHead = A;
        while(curHead != null && curTail != curHead) {//前者是奇数个节点 后者是偶数个节点
            if(curTail.val != curHead.val) {
                return false;
            }
            curTail = curTail.next;
            curHead = curHead.next;
        }
        return true;
    }
}
